package com.person.android.domain.email;

import com.person.android.utils.LogUtils;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * @autor zhaokang.
 * @time on 2015/12/19.
 */
public class EMailSender {
    private String mHost;
    private String mPort;
    private String mUserName;
    private String mPassword;
    private String[] mImages;

    public String[] getImagePath() {
        return mImages;
    }

    public void setImagePath(String[] imagePath) {
        this.mImages = imagePath;
    }

    public EMailSender(String host, String port, String userName, String password) {
        this.mHost = host;
        this.mPort = port;
        this.mUserName = userName;
        this.mPassword = password;
    }

    public void sendEmail(String subject, String reception, String sender, String content) {
        Properties props = new Properties();
        props.put("mail.smtp.host", mHost);  //设置smtp的服务器地址
        props.put("mail.smtp.socketFactory.mPort", mPort);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.mPort", mPort);

        // 返回授权Base64编码
        PopupAuthenticator auth = new PopupAuthenticator(mUserName, mPassword);
        // 获取会话对象
        Session session = Session.getInstance(props, auth);
        // 设置为DEBUG模式
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        try {
            message.setSubject(subject);
            message.setSentDate(new Date());
            message.setFrom(new InternetAddress(sender));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(reception));
            // 邮件文本/HTML内容
            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(content, "text/html");
            multipart.addBodyPart(messageBodyPart);

            // 添加邮件附件
            if (mImages != null && mImages.length > 0) {
                for (String filePath : mImages) {
                    MimeBodyPart attachPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(filePath);
                    attachPart.setDataHandler(new DataHandler(source));
                    attachPart.setFileName(filePath);
                    multipart.addBodyPart(attachPart);
                }
            }

            // 保存邮件内容
            message.setContent(multipart);
            // 获取SMTP协议客户端对象，连接到指定SMPT服务器
            Transport transport = session.getTransport("smtp");
            transport.connect(mHost, Integer.parseInt(mPort), mUserName, mPassword);

            LogUtils.logInfo(LogUtils.FileListActivityFlag, "connet it success!!!!");

            //发送邮件到SMTP服务器
            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
            Transport.send(message);
            LogUtils.logInfo(LogUtils.FileListActivityFlag, "send it success!!!!");

            //关闭连接
            transport.close();

        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.logInfo(LogUtils.FileListActivityFlag, "email exception!!!");
        }

    }
}
